package com.qf.listener;

import com.qf.config.RabbitMQConfig;
import com.qf.constant.RabbitConstant;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * projectName:temp_shop
 * author:luo
 * time:2023/3/14 21:33
 */
@Component
public class MyListener {
    /**
     * 监听queue1
     */
    @RabbitListener(queues = "queue1")
    public void receiverQueue1(String name) {
        System.out.println("1号MyListener接收到的消息" + name);
    }

    //    @RabbitListener(queues = "queue1")
//    public void receiverQueue2(Map map){
//        System.out.println("2号MyListener接收到的消息"+map);
//    }
    @RabbitListener(queues = "queue2")
    public void receiverQueue2(String name) {
        System.out.println("2号MyListener接收到的消息" + name);
    }

    @RabbitListener(queues = "Q1")
    public void receiverQ1(String name) {
        System.out.println("Q1号MyListener接收到的消息" + name);
    }

    @RabbitListener(queues = "Q2")
    public void receiverQ2(String name) {
        System.out.println("Q2号MyListener接收到的消息" + name);
    }

    /**
     * 注解测试
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = RabbitConstant.QUEUE_1_ANNO, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = RabbitConstant.EXCHANGE_TOPIC_ANNO, type = ExchangeTypes.TOPIC),
            key = {"*.orange.*"})})
    public void receiverQueue1Anno(String msg) {
        System.out.println("Queue1Anno接收消息" + msg);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = RabbitConstant.QUEUE_2_ANNO, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = RabbitConstant.EXCHANGE_TOPIC_ANNO, type = ExchangeTypes.TOPIC),
            key = {RabbitConstant.ROUTINGKEY2_ANNO, RabbitConstant.ROUTINGKEY3_ANNO})})
    public void receiverQueue2Anno(String msg) {
        System.out.println("Queue2Anno接收消息" + msg);
    }

    /**
     * 测试Java配置类创建交换机，队列，绑定
     */
    @RabbitListener(queues = {RabbitMQConfig.QUEUE_1})
    public void receiverJavaConfigurationQueue1(String msg) {
        System.out.println("JavaConfigurationQueue1接收消息" + msg);
    }

    @RabbitListener(queues = {"#{queue2.name}"})
    public void receiverJavaConfigurationQueue2(String msg) {
        System.out.println("JavaConfigurationQueue2接收消息" + msg);
    }

    /**
     * 测试消费者手动应答
     */
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(name = "queue_c1", durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = "exchange_c"),
            key = {"c", RabbitConstant.ROUTINGKEY3_ANNO})})
    public void receiverC1(String msg, Channel channel, Message message) {

        try {
            //1接收消息
            //2打印 8行
            System.out.println("receiverC1接收消息" + msg);
            //3抛出异常
            int i = 1 / 0;
            //4...
            //5手动应答
            //long deliveryTag,
            // boolean multiple
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //抛出异常
            //判断当前消息是否已经投递过了
            if (message.getMessageProperties().isRedelivered()) {
                System.out.println("第二次是发过来的消息了");
                //是投递过的，第二次是发过来的消息了
                //拒绝接收消息
                try {
                    //打印日志
                    //人工介入
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);

                } catch (IOException ex) {
                    //打印日志
                    //人工介入
                }
            } else {
                //是第一次发过来的
                System.out.println("第一次是发过来的消息了");
                try {
                    //不执行手动确认机制
                    //long deliveryTag,
                    // boolean multiple
                    //boolean requeue 是否将当前消息放回队列 是否染队列再投递一次
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

        }
    }

}
